package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisRedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;

    //获取攻略统计对象
    @Override
    public StrategyStatisVO getStrategyStatisVO(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO vo = null;
        if (!template.hasKey(key)) {
            //key不存在
            //初始化vo对象
            vo = new StrategyStatisVO();
            //取出5个统计数据
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            //把vo对象设置进缓存
            template.opsForValue().set(key, JSON.toJSONString(vo));
        } else {
            //key存在,获取vo对象
            String vos = template.opsForValue().get(key);
            //转为json格式
            vo = JSON.parseObject(vos, StrategyStatisVO.class);
        }
        return vo;
    }

    //阅读数+1
    @Override
    public void viewnumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        //无论有无对象,都统一加
        vo.setViewnum(vo.getViewnum() + num);
        //设置攻略统计对象
        this.setStrategyStatisVO(vo);

    }

    //设置攻略统计对象
    @Override
    public StrategyStatisVO setStrategyStatisVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
        return vo;
    }

    //评论数+1
    @Override
    public void replaynumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        vo.setReplynum(vo.getReplynum() + num);
        this.setStrategyStatisVO(vo);
    }


    //用户攻略收藏
    @Override
    public Boolean favor(String sid, String uid) {
       //获取记号,sign记号
        String signkey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //调用方法,获取用户收藏id集合方法
        List<String> sidList = this.getUserFavorList(sid, uid);
        //获取redis中的vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        //判断sid是否在用户sidList中
        if (sidList.contains(sid)) {
            //取消收藏,-1,删除sid
            vo.setFavornum(vo.getFavornum() - 1);
            sidList.remove(sid);
        } else {
            //收藏,+1,添加sid
            vo.setFavornum(vo.getFavornum() + 1);
            sidList.add(sid);
        }
        //设置进redis中
        template.opsForValue().set(signkey, JSON.toJSONString(sidList));
        //更新vo对象
        this.setStrategyStatisVO(vo);
        return sidList.contains(sid);
    }

    //用户收藏id集合
    @Override
    public List<String> getUserFavorList(String sid, String userId) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        //不存在,做初始化操作
        List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)) {
            //有key直接获取
            String sidListStr = template.opsForValue().get(key);
            sidList = JSON.parseArray(sidListStr, String.class);
        }
        return sidList;
    }


    //点赞数统计
    @Override
    public Boolean strategyThumbup(String sid, String uid) {
        //获取记号,sign记号
        String signkey = RedisKeys.USER_STRATEGY_THUMBUP.join(sid,uid);
        //不存在则获取,设置进vo对象
        if (!template.hasKey(signkey)) {
            //获取vo对象
            StrategyStatisVO vo = this.getStrategyStatisVO(sid);
            //点赞数+1
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            //重新设置vo对象存进redis中
            this.setStrategyStatisVO(vo);
            //创建现时间和结束时间,今天最后一秒
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);
            //获取两个时间的间隔(秒)
            long time = DateUtil.getDateBetween(now, end);
            //设置key存活时间
            template.opsForValue().set(signkey,"1",time,TimeUnit.SECONDS);
            //不存在,返回true让前端弹出收藏成功信息操作
            return true;
        }
        //存在,不做任何操作
        return false;
    }


    //判断攻略是否存在
    @Override
    public boolean isStrategyExist(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        return template.hasKey(key);
    }

    //获取所有的vo对象
    @Override
    public List<StrategyStatisVO> queryStatisVOS() {
        //拼接任意的key
        String keys = RedisKeys.STRATEGY_STATIS_VO.join("*");
        //根据keys获取所有vo对象
        Set<String> vos = template.keys(keys);
        //创建list存放vo对象数据
        ArrayList<StrategyStatisVO> list = new ArrayList<>();
        //判断vos是否有值
        if (vos != null && vos.size() > 0){
            //遍历vos对象
            for (String vo : vos) {
                //获取对应的vo数据
                String value = template.opsForValue().get(vo);
                //逐个存进list中
                list.add(JSON.parseObject(value,StrategyStatisVO.class));
            }
        }
        return list;
    }
}
